home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch1 / Thumbs.frm < prev    next >
Text File  |  1999-04-25  |  12KB  |  402 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Thumbs"
  4.    ClientHeight    =   5685
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1800
  7.    ClientWidth     =   8715
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   379
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   581
  13.    Begin VB.FileListBox filFiles 
  14.       Height          =   1065
  15.       Left            =   0
  16.       TabIndex        =   5
  17.       Top             =   1920
  18.       Width           =   2175
  19.    End
  20.    Begin VB.ComboBox cboPatterns 
  21.       Height          =   315
  22.       Left            =   0
  23.       TabIndex        =   4
  24.       Text            =   "PatternCombo"
  25.       Top             =   3240
  26.       Width           =   2175
  27.    End
  28.    Begin VB.PictureBox picHidden 
  29.       AutoSize        =   -1  'True
  30.       BorderStyle     =   0  'None
  31.       Height          =   960
  32.       Left            =   4200
  33.       ScaleHeight     =   64
  34.       ScaleMode       =   3  'Pixel
  35.       ScaleWidth      =   64
  36.       TabIndex        =   3
  37.       Top             =   480
  38.       Visible         =   0   'False
  39.       Width           =   960
  40.    End
  41.    Begin VB.PictureBox picThumb 
  42.       AutoRedraw      =   -1  'True
  43.       BorderStyle     =   0  'None
  44.       Height          =   1560
  45.       Index           =   0
  46.       Left            =   2235
  47.       ScaleHeight     =   104
  48.       ScaleMode       =   3  'Pixel
  49.       ScaleWidth      =   104
  50.       TabIndex        =   2
  51.       Top             =   0
  52.       Visible         =   0   'False
  53.       Width           =   1560
  54.    End
  55.    Begin VB.DriveListBox drvDrives 
  56.       Height          =   315
  57.       Left            =   0
  58.       TabIndex        =   1
  59.       Top             =   0
  60.       Width           =   2175
  61.    End
  62.    Begin VB.DirListBox dirDirectories 
  63.       Height          =   1155
  64.       Left            =   0
  65.       TabIndex        =   0
  66.       Top             =   360
  67.       Width           =   2175
  68.    End
  69.    Begin VB.Label lblThumb 
  70.       Alignment       =   2  'Center
  71.       BeginProperty Font 
  72.          Name            =   "Arial"
  73.          Size            =   8.25
  74.          Charset         =   0
  75.          Weight          =   400
  76.          Underline       =   0   'False
  77.          Italic          =   0   'False
  78.          Strikethrough   =   0   'False
  79.       EndProperty
  80.       Height          =   255
  81.       Index           =   0
  82.       Left            =   2235
  83.       TabIndex        =   6
  84.       Top             =   1560
  85.       Visible         =   0   'False
  86.       Width           =   1560
  87.    End
  88.    Begin VB.Menu mnuFile 
  89.       Caption         =   "&File"
  90.       Begin VB.Menu mnuFileExit 
  91.          Caption         =   "E&xit"
  92.       End
  93.    End
  94.    Begin VB.Menu mnuThumbs 
  95.       Caption         =   "&Thumbs"
  96.       Begin VB.Menu mnuThumbsShow 
  97.          Caption         =   "&Show"
  98.          Shortcut        =   {F5}
  99.       End
  100.       Begin VB.Menu mnuThumbsSize 
  101.          Caption         =   "S&ize"
  102.          Begin VB.Menu mnuThumbsSetSize 
  103.             Caption         =   "&Small"
  104.             Index           =   50
  105.             Shortcut        =   ^S
  106.          End
  107.          Begin VB.Menu mnuThumbsSetSize 
  108.             Caption         =   "&Medium"
  109.             Index           =   100
  110.             Shortcut        =   ^M
  111.          End
  112.          Begin VB.Menu mnuThumbsSetSize 
  113.             Caption         =   "&Large"
  114.             Index           =   200
  115.             Shortcut        =   ^L
  116.          End
  117.       End
  118.    End
  119. End
  120. Attribute VB_Name = "Form1"
  121. Attribute VB_GlobalNameSpace = False
  122. Attribute VB_Creatable = False
  123. Attribute VB_PredeclaredId = True
  124. Attribute VB_Exposed = False
  125. Option Explicit
  126.  
  127. Private Running As Boolean
  128. Private DirName As String
  129. Private MaxFileNum As Integer
  130. Private SelectedThumb As Integer
  131. Private ThumbSize As Single
  132.  
  133. Private Type SHFILEOPSTRUCT
  134.     hwnd As Long
  135.     wFunc As Long
  136.     pFrom As String
  137.     pTo As String
  138.     fFlags As Integer
  139.     fAnyOperationsAborted As Long
  140.     hNameMappings As Long
  141.     lpszProgressTitle As Long '  only used if FOF_SIMPLEPROGRESS
  142. End Type
  143. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
  144. Private Const FO_DELETE = &H3
  145. Private Const FOF_ALLOWUNDO = &H40
  146. Private Const FOF_NOCONFIRMATION = &H10
  147.  
  148. ' Move the file into the wastebasket.
  149. Private Sub DeleteFile(ByVal Index As Integer)
  150. Dim op As SHFILEOPSTRUCT
  151. Dim file_name As String
  152.  
  153.     file_name = DirName & lblThumb(Index).Caption
  154.  
  155.     file_name = DirName & lblThumb(Index).Caption
  156.     With op
  157.         .wFunc = FO_DELETE
  158.         .pFrom = file_name
  159.         .fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION
  160.     End With
  161.     SHFileOperation op
  162.  
  163.     If Not op.fAnyOperationsAborted Then
  164.         ' Mark the file as deleted.
  165.         lblThumb(Index).Caption = ""
  166.         picThumb(Index).Line (0, 0)- _
  167.             (picThumb(Index).ScaleWidth, _
  168.              picThumb(Index).ScaleHeight)
  169.         picThumb(Index).Line _
  170.             (picThumb(Index).ScaleWidth, 0)- _
  171.             (0, picThumb(Index).ScaleHeight)
  172.     End If
  173. End Sub
  174.  
  175. ' Display thumbnails for this directory.
  176. Private Sub ShowThumbs()
  177. Const GAP = 2
  178.  
  179. Dim i As Integer
  180. Dim new_name As String
  181. Dim wid As Single
  182. Dim hgt As Single
  183. Dim thumb_left As Single
  184. Dim thumb_top As Single
  185.  
  186.     MaxFileNum = 0
  187.     SelectedThumb = -1
  188.  
  189.     ' Get the directory name.
  190.     DirName = dirDirectories.Path
  191.     If Right$(DirName, 1) <> "\" Then
  192.         DirName = DirName & "\"
  193.     End If
  194.  
  195.     ' Hide the thumbnail pictures.
  196.     For i = 0 To picThumb.UBound
  197.         picThumb(i).Visible = False
  198.         lblThumb(i).Visible = False
  199.     Next i
  200.  
  201.     ' See where the first thumb goes.
  202.     thumb_left = picThumb(0).Left
  203.     thumb_top = picThumb(0).Top
  204.  
  205.     ' Get the file names.
  206.     For i = 0 To filFiles.ListCount - 1
  207.         new_name = filFiles.List(i)
  208.  
  209.         ' Load the file.
  210.         On Error Resume Next
  211.         picHidden.Picture = LoadPicture(DirName & new_name)
  212.         If Err.Number = 0 Then
  213.             ' We loaded the picture successfully.
  214.             ' Display its thumbnail.
  215.             On Error GoTo 0
  216.  
  217.             ' Calculate the thumbnail size.
  218.             wid = picHidden.ScaleWidth
  219.             hgt = picHidden.ScaleHeight
  220.             If wid > ThumbSize Then
  221.                 hgt = hgt * ThumbSize / wid
  222.                 wid = ThumbSize
  223.             End If
  224.             If hgt > ThumbSize Then
  225.                 wid = wid * ThumbSize / hgt
  226.                 hgt = ThumbSize
  227.             End If
  228.  
  229.             ' Load the thumbnail picture.
  230.             If MaxFileNum > picThumb.UBound Then
  231.                 Load picThumb(MaxFileNum)
  232.                 Load lblThumb(MaxFileNum)
  233.             End If
  234.  
  235.             ' Display the thumbnail.
  236.             picThumb(MaxFileNum).BorderStyle = vbBSNone
  237.             picThumb(MaxFileNum).Move _
  238.                 thumb_left, thumb_top, _
  239.                 ThumbSize, ThumbSize
  240.             picThumb(MaxFileNum).Line (0, 0)-(picThumb(MaxFileNum).ScaleWidth, picThumb(MaxFileNum).ScaleHeight), vbWhite, BF
  241.             picThumb(MaxFileNum).PaintPicture _
  242.                 picHidden.Picture, _
  243.                 (ThumbSize - wid) / 2, _
  244.                 (ThumbSize - hgt) / 2, wid, hgt, _
  245.                 0, 0, picHidden.ScaleWidth, picHidden.ScaleHeight
  246.             picThumb(MaxFileNum).Visible = True
  247.  
  248.             lblThumb(MaxFileNum).Move _
  249.                 thumb_left, thumb_top + ThumbSize, _
  250.                 ThumbSize
  251.             lblThumb(MaxFileNum).Caption = new_name
  252.             lblThumb(MaxFileNum).Visible = True
  253.  
  254.             MaxFileNum = MaxFileNum + 1
  255.  
  256.             ' See where the next thumb goes.
  257.             thumb_left = thumb_left + ThumbSize + GAP
  258.             If thumb_left + ThumbSize > ScaleWidth Then
  259.                 thumb_left = picThumb(0).Left
  260.                 thumb_top = thumb_top + ThumbSize + _
  261.                     lblThumb(0).Height + 3 * GAP
  262.                 If thumb_top + ThumbSize > ScaleHeight Then Exit For
  263.             End If
  264.  
  265.             DoEvents
  266.             If Not Running Then Exit Sub
  267.         End If ' End if we got no error loading the picture.
  268.     Next i
  269. End Sub
  270. ' The user selected a directory. Let the filFiles
  271. ' control know so it can update its list.
  272. Private Sub dirDirectories_Change()
  273.     filFiles.Path = dirDirectories.Path
  274. End Sub
  275.  
  276. ' The user selected a drive. Let the dirDirectories
  277. ' control know so it can update its list.
  278. Private Sub drvDrives_Change()
  279.     'On Error GoTo DriveError
  280.     dirDirectories.Path = drvDrives.Drive
  281.     Exit Sub
  282.  
  283. DriveError:
  284.     drvDrives.Drive = dirDirectories.Path
  285.     Exit Sub
  286. End Sub
  287.  
  288.  
  289. ' Create the list of file patterns.
  290. Private Sub Form_Load()
  291.     dirDirectories.Path = App.Path
  292.  
  293.     cboPatterns.AddItem "Bitmaps (*.bmp)"
  294.     cboPatterns.AddItem "GIFs (*.gif)"
  295.     cboPatterns.AddItem "JPEGs (*.jpg)"
  296.     cboPatterns.AddItem "Icons (*.ico)"
  297.     cboPatterns.AddItem "Cursors (*.cur)"
  298.     cboPatterns.AddItem "Run-Length Encoded (*.rle)"
  299.     cboPatterns.AddItem "Metafiles (*.wmf)"
  300.     cboPatterns.AddItem "Enhanced Metafiles (*.emf)"
  301.     cboPatterns.AddItem "Graphic Files (*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf)"
  302.     cboPatterns.AddItem "All Files (*.*)"
  303.  
  304.     cboPatterns.ListIndex = 8
  305.  
  306.     mnuThumbsSetSize_Click 100
  307. End Sub
  308. ' Make the controls fill the form.
  309. Private Sub Form_Resize()
  310. Const GAP = 2
  311.  
  312. Dim wid As Integer
  313. Dim hgt As Integer
  314.  
  315.     If WindowState = vbMinimized Then Exit Sub
  316.  
  317.     wid = drvDrives.Width
  318.     drvDrives.Move GAP, GAP, wid
  319.     cboPatterns.Move GAP, ScaleHeight - cboPatterns.Height, wid
  320.  
  321.     hgt = (cboPatterns.Top - drvDrives.Top - drvDrives.Height - 3 * GAP) / 2
  322.     If hgt < 100 Then hgt = 100
  323.     dirDirectories.Move GAP, drvDrives.Top + drvDrives.Height + GAP, wid, hgt
  324.     filFiles.Move GAP, dirDirectories.Top + dirDirectories.Height + GAP, wid, hgt
  325. End Sub
  326.  
  327. Private Sub mnuFileExit_Click()
  328.     Unload Me
  329. End Sub
  330.  
  331. ' Set the thumbnail size.
  332. Private Sub mnuThumbsSetSize_Click(Index As Integer)
  333.     mnuThumbsSetSize(50).Checked = False
  334.     mnuThumbsSetSize(100).Checked = False
  335.     mnuThumbsSetSize(200).Checked = False
  336.     mnuThumbsSetSize(Index).Checked = True
  337.  
  338.     ThumbSize = Index
  339.  
  340.     mnuThumbsShow_Click
  341. End Sub
  342. ' Start or stop displaying thumbnails.
  343. Private Sub mnuThumbsShow_Click()
  344.     If Running Then
  345.         ' Stop.
  346.         mnuThumbsShow.Enabled = False
  347.         mnuThumbsShow.Caption = "Stopping"
  348.         Running = False
  349.         DoEvents
  350.     Else
  351.         ' Start.
  352.         mnuThumbsShow.Caption = "Stop"
  353.         Running = True
  354.         MousePointer = vbHourglass
  355.         DoEvents
  356.  
  357.         ShowThumbs
  358.  
  359.         Running = False
  360.         mnuThumbsShow.Caption = "Show"
  361.         mnuThumbsShow.Enabled = True
  362.         MousePointer = vbDefault
  363.     End If
  364. End Sub
  365. ' The user selected a pattern. Let the filFiles
  366. ' control know so it can filter its list.
  367. Private Sub cboPatterns_Click()
  368. Dim pat As String
  369. Dim p1 As Integer
  370. Dim p2 As Integer
  371.  
  372.     pat = cboPatterns.List(cboPatterns.ListIndex)
  373.     p1 = InStr(pat, "(")
  374.     p2 = InStr(pat, ")")
  375.     filFiles.Pattern = Mid$(pat, p1 + 1, p2 - p1 - 1)
  376. End Sub
  377.  
  378. ' The user clicked on a thumbnail. Select it.
  379. Private Sub picThumb_Click(Index As Integer)
  380.     If SelectedThumb >= 0 Then
  381.         picThumb(SelectedThumb).BorderStyle = vbBSNone
  382.     End If
  383.  
  384.     SelectedThumb = Index
  385.     picThumb(SelectedThumb).BorderStyle = vbFixedSingle
  386.  
  387.     Caption = "Thumbs - " & lblThumb(SelectedThumb).Caption
  388. End Sub
  389.  
  390.  
  391. ' The user pressed a key while a thumbnail had
  392. ' the focus. If it is the delete key, move the
  393. ' file into the waste basket.
  394. Private Sub picThumb_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
  395.     If (KeyCode = vbKeyDelete) And _
  396.        (Len(lblThumb(Index).Caption) > 0) _
  397.     Then
  398.         ' Move the file into the wastebasket.
  399.         DeleteFile Index
  400.     End If
  401. End Sub
  402.